#include "GPRS.h"

STATUS TCP_Init(void)
{
    delay(2000);	
    delay(2000);
	
    //AT_SendCmd(AT, AT_OK,1000);  //测试AT
    delay(2000);

    AT_SendCmd(AT_CPIN_ASK, "+CPIN:READY",1000); //确保SIM卡PIN码解锁，返回READY，表示解锁成功
    delay(1500);

    AT_SendCmd(AT_CREG_ASK, "OK",1000);    //确认网络搜索成功,OK
    delay(1000);

    AT_SendCmd(AT_CSQ, "OK", 1000);//查看信号质量
    delay(1000);
    //SendCmd(CGDCONT,"OK", 1000);//
    //Delay(1000);
    AT_SendCmd(AT_CGACT_ACT, "OK", 1000);//激活
    delay(1000);

    AT_SendCmd(AT_CGATT_ON, "OK", 1000);  //
    delay(1000);
    AT_SendCmd(AT_CMMUX_0, "OK", 1000);  //single way
    delay(1000);
    AT_SendCmd(AT_CMMODE_1, "OK",1000);   //配置透传
    delay(1000);
	
    //SendCmd("AT+IPSTART=\"TCP\",\"183.230.40.33\",80\r\n","CONNECT",1000); //连接EDP服务器
    AT_SendCmd(AT_IPSTART_SERVER, "CONNECT",3000);//AT+IPSTART="TCP","183.230.40.39",876
    delay(1000);
    
    return ok;
}
uint32_t TCP_SendData(uint8_t *buf, uint16_t len)
{
    //UART3_SendData(buf, len);
  UART3_SendStr(buf);  
	return len;
}

STATUS AT_SendCmd(const char *AT_Cmd, const char *key, uint32_t mDelay)
{
    uint8_t buf[2048];
    uint16_t len;
    while(1)
    {
        UART3_Clear();

        UART3_SendStr(AT_Cmd);

        delay(mDelay);

        UART3_GetData(buf,&len);
			UART1_SendStr("Reply:");
			UART1_SendStr(buf);
				char *p;
				p = strstr(buf, AT_ERROR);
        if((NULL != p))
        {
            UART1_SendStr("AT_Cmd error:");
            UART1_SendStr(AT_Cmd);
            UART1_SendStr("\n");
            continue;
        }
				
				p = strstr(buf, key);
        if(NULL != p)
        {
            break;
        }
        else
        {
            delay(100);
        }
    }
    return ok;
}
